// 书铺地址
$("#storeAddress").change(e => {
    var storeAddress = $("#storeAddress").val();
    $("span[name='storeAddress']").text(storeAddress);
});

// 书铺名称
$("#storeName").change(e => {
    var storeName = $("#storeName").val();
    $("span[name='storeName']").text(storeName);
});

// 书铺手机号
$("#phone").change(e => {
    var phone = $("#phone").val();
    $("span[name='phone']").text(phone);
});

// 退货地址
$("#shippingAddress").change(e => {
    var shippingAddress = $("#shippingAddress").val();
    $("div[name='shippingAddress']>p").text(shippingAddress);
});

// 收件人
$("#addressee").change(e => {
    var addressee = $("#addressee").val();
    $("div[name='addressee']>p").text(addressee);
});

// 联系方式
$("#addressPhone").change(e => {
    var addressPhone = $("#addressPhone").val();
    $("div[name='addressPhone']>p").text(addressPhone);
});

// 添加书铺
function registerStore() {
    var storeAddress = $("span[name='storeAddress']").text();
    var phone = $("span[name='phone']").text();
    var storeName = $("span[name='storeName']").text();
    var shippingAddress = $("div[name='shippingAddress']>p").text();
    var addressee = $("div[name='addressee']>p").text();
    var addressPhone = $("div[name='addressPhone']>p").text();

    var result = "";
    $.ajax({
        type: "post",
        url: "/api/bookStore/u/addStore",
        dataType: "text",
        async: false,
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify({
            "storeName": storeName,
            "storePhone": phone,
            "storeAddress": storeAddress
        }),
        success: data => {
            data = JSON.parse(data);
            if (data.success) {
                var bookStore = JSON.parse(data.data);
                var storeAddress = JSON.stringify({
                    "storeId": bookStore.storeId,
                    "address": shippingAddress,
                    "phone": addressPhone,
                    "addressee": addressee
                });
                result = addAddress(storeAddress);
            }else {
                $.confirm({
                    type: 'red',
                    animationSpeed: 300,
                    title: false,
                    content: data.msg,
                    buttons: {
                        cancel: {
                            text: '确认',
                            btnClass: 'waves-effect waves-button'
                        }
                    }
                });
            }
            console.log(result);
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    });
    return result;
}

function addAddress(data) {
    var result;
    $.ajax({
        type: "post",
        url: "/api/address/u/addStoreAddress",
        dataType: "text",
        async: false,
        contentType: "application/json;charset=UTF-8",
        data: data,
        success: data => {
            data = JSON.parse(data);
            if (data.success) {
                $.confirm({
                    type: 'blue',
                    animationSpeed: 300,
                    title: false,
                    content: '申请成功',
                    buttons: {
                        cancel: {
                            text: '确认',
                            btnClass: 'waves-effect waves-button'
                        }
                    }
                });
            }else {
                $.confirm({
                    type: 'red',
                    animationSpeed: 300,
                    title: false,
                    content: data.msg,
                    buttons: {
                        cancel: {
                            text: '确认',
                            btnClass: 'waves-effect waves-button'
                        }
                    }
                });
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const msg = xhr.responseText;
            console.log(msg);
        }
    });
    return result;
}

$(document).ready(function () {
    var storeAddress = $("#storeAddress").val();
    $("span[name='storeAddress']").text(storeAddress);

    var phone = $("#phone").val();
    $("span[name='phone']").text(phone);

    var storeName = $("#storeName").val();
    $("span[name='storeName']").text(storeName);

    var shippingAddress = $("#shippingAddress").val();
    $("div[name='shippingAddress']>p").text(shippingAddress);

    var addressee = $("#addressee").val();
    $("div[name='addressee']>p").text(addressee);

    var addressPhone = $("#addressPhone").val();
    $("div[name='addressPhone']>p").text(addressPhone);
})